import 'package:dy_download_utils/enum_connect_state.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'snackbar_request_loading.dart';

class SnackContentConfig {
  String? waitingMessage;
  Widget? waitingIcon;
  Text? waitingMessageText;
  String? doneMessage;
  Widget? doneIcon;
  Text? doneMessageText;
  String? errMessage;
  Widget? errIcon;
  Text? errMessageText;

  SnackContentConfig(
      {this.waitingMessage,
      this.waitingIcon,
      this.waitingMessageText,
      this.doneMessage,
      this.doneIcon,
      this.doneMessageText,
      this.errMessage,
      this.errIcon,
      this.errMessageText});
}

GetSnackBar buildRequestGetBar<T>(SnackContentConfig config,
    ConnectState Function() buildState, T Function() buildValue,
    {SnackbarStatusCallback? snackbarStatus}) {
  return GetSnackBar(
    messageText: Obx(() {
      var state = buildState();
      Widget widget = SnackBarContent.Waiting.buildWidget(
        message: config.waitingMessage,
        messageText: config.waitingMessageText,
        icon: config.waitingIcon,
      );
      if (state == ConnectState.done) {
        Future.delayed(
            Duration(milliseconds: 1500), () => Get.back(result: buildValue()));
        widget = SnackBarContent.Done.buildWidget(
          message: config.doneMessage,
          messageText: config.doneMessageText,
          icon: config.doneIcon,
        );
      } else if (state == ConnectState.err) {
        Future.delayed(
            Duration(milliseconds: 1500), () => Get.back(result: buildValue()));
        widget = SnackBarContent.Error.buildWidget(
          message: config.errMessage,
          messageText: config.errMessageText,
          icon: config.errIcon,
        );
      }
      return AnimatedSwitcher(
        duration: Duration(milliseconds: 200),
        transitionBuilder: (Widget child, Animation<double> animation) {
          return FadeTransition(
            opacity: animation,
            child: child,
          );
        },
        child: widget,
      );
    }),
    borderRadius: 10.0,
    margin: EdgeInsets.all(10.0),
    padding: EdgeInsets.zero,
    backgroundColor: Get.theme.primaryColor,
    isDismissible: false,
    duration: Duration(milliseconds: 50000),
    animationDuration: Duration(milliseconds: 200),
    showProgressIndicator: false,
    snackPosition: SnackPosition.TOP,
    snackbarStatus: snackbarStatus ?? null,
  );
}
